<template>
  <div class="app-container">
    <el-card shadow="always">
      <queryForm
        :loading="loading"
        :query-params="queryParams"
        @updateList="getList()"
        @reset="reset('queryForm')"
      />
    </el-card>

    <el-col class="btn-group">
      <div>
        <el-button
          class="bg-color-2d"
          size="medium"
          @click="handleExport"
          v-hasPermi="['system:checklist:export']"
          >导出</el-button
        >
        <el-button
          class="bg-color-2d"
          size="medium"
          @click="handleExport"
          v-hasPermi="['system:checklist:export']"
          >生成日志</el-button
        >
      </div>
      <el-button type="primary" size="medium" @click="$router.push('examine/add')">新增</el-button>
    </el-col>

    <el-card shadow="always">
      <el-table v-loading="loading" :data="checklistList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" type="index" />
        <el-table-column label="类型" align="center" prop="state" />
        <el-table-column label="涉及领域" align="center" />
        <el-table-column label="当事人" align="center" prop="userNames" />
        <el-table-column label="联系电话" align="center" prop="phone" />
        <el-table-column label="文书编号" align="center" />
        <el-table-column label="复查时间" align="center">
          <template slot-scope="scope">
            {{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}
          </template>
        </el-table-column>

        <el-table-column label="检查地址" align="center" prop="censor" />
        <el-table-column label="现场照片" align="center">
          <template slot-scope="scope">
            <image-preview
              v-if="scope.row.checkImg"
              :src="scope.row.checkImg"
              :height="60"
            ></image-preview>
            <span v-else>暂无图片</span>
          </template>
        </el-table-column>
        <el-table-column label="立案统计" align="center" prop="remark" />
        <el-table-column label="执法人员" align="center" prop="censorName" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:checklist:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:checklist:remove']"
              >详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改检查列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="人员名称" prop="userNames">
          <el-input v-model="form.userNames" type="input" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择">
            <el-option
              v-for="item in stateOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检查名称" prop="censorName">
          <el-input v-model="form.censorName" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="检查地址" prop="censor">
          <el-input v-model="form.censor" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="照片地址" prop="checkImg">
          <image-upload v-model="form.checkImg"></image-upload>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listChecklist,
  getChecklist,
  delChecklist,
  addChecklist,
  updateChecklist
} from '@/api/examine'
import queryForm from './components/queryForm'

export default {
  components: { queryForm },
  name: 'Checklist',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检查列表表格数据
      checklistList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userNames: null,
        phone: null,
        state: null,
        censorName: null,
        censor: null,
        checkImg: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userNames: {
          message: '人员名称不能为空',
          required: true
        },
        phone: {
          pattern: /^1((34[0-8])|(8\d{2})|(([35][0-35-9]|4[579]|66|7[35678]|9[1389])\d{1}))\d{7}$/,
          message: '请输入正确的手机号码',
          required: true
        },
        state: {
          message: '状态不能为空',
          required: true
        },
        censorName: {
          message: '检查名称不能为空',
          required: true
        },
        censor: {
          message: '检查地址不能为空',
          required: true
        }
      },
      stateOptions: [
        { value: 0, label: '异常' },
        { value: 1, label: '举报' }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询检查列表列表 */
    getList() {
      this.loading = true
      listChecklist(this.queryParams).then((response) => {
        response.rows.forEach((item) => {
          switch (item.state) {
            case 0:
              item.state = '异常'
              break
            case 1:
              item.state = '举报'
              break
          }
        })
        this.checklistList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset(ref) {
      if (ref && ref == 'queryForm') {
        this.queryParams = { pageNum: 1, pageSize: 10 }
        this.resetForm(ref)
        return
      }
      this.form = {
        checkId: null,
        userNames: null,
        phone: null,
        state: null,
        censorName: null,
        censor: null,
        checkImg: null,
        createTime: null,
        updateBy: null,
        createBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm('form')
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.checkId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const checkId = row.checkId || this.ids
      getChecklist(checkId).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改检查列表'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.checkId != null) {
            updateChecklist(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addChecklist(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'system/checklist/export',
        {
          ...this.queryParams
        },
        `行政检查数据.xlsx`
      )
    }
  }
}
</script>
<style lang="scss" scoped>
.btn-group {
  display: flex;
  justify-content: space-between;
  margin: 10px 0 !important;
}
</style>
